<?php

class Roles_Model_Perfilrol extends Model_Cna {
	
	public function guardarPerfilRol($codrol, $perfiles){
		$perfilrolTable = new Roles_Model_DbTable_Perfilrol();
		$perfilrolTable->getAdapter()->beginTransaction();
		try {
			$perfilrolTable->delete("codrol=".$codrol);
			$datos=array();
			
			foreach ( $perfiles as $codigo ) {
				$datos ["codseccion"] = $codigo;
				$datos ["codrol"] = $codrol;
				$datos ["ver"] = "S";
				$datos ["crear"] = "S";
				$datos ["modificar"] = "S";
				$datos ["eliminar"] = "S";
				$perfilrolTable->createRow($datos)->save();
			}
			$perfilrolTable->getAdapter()->commit();
		} catch (Exception $e) {
			$perfilrolTable->getAdapter()->rollBack();
			throw $e;
		}
		
	}
	
	public function guardarPermisosRol($codrol, $arr_asignar){
		$perfilrolTable = new Roles_Model_DbTable_Perfilrol();
		$perfilrolTable->getAdapter()->beginTransaction();
		try {
			/*Update*/
			$datos=array();
			$datos["ver"] 		 = "N";
			$datos["crear"] 	 = "N";
			$datos["modificar"]  = "N";
			$datos["eliminar"]   = "N";
			$where = $perfilrolTable->getAdapter()->quoteInto("codseccion IN (?)",array_keys($arr_asignar));
			$where.= " AND ".$perfilrolTable->getAdapter()->quoteInto("codrol=?",$codrol);
			$perfilrolTable->update($datos, $where);
			/*Fin update*/
			
			
			foreach ($arr_asignar as $key => $value) {
				$datos=array();
				foreach ($value as $accion) {
					$datos[$accion]="S";
				}
				$where = $perfilrolTable->getAdapter()->quoteInto("codseccion = ?",$key);
				$where.= " AND ".$perfilrolTable->getAdapter()->quoteInto("codrol=?",$codrol);
				$perfilrolTable->update($datos, $where);
			}
			
			$perfilrolTable->getAdapter()->commit();
		} catch (Exception $e) {
			$perfilrolTable->getAdapter()->rollBack();
			throw $e;
		}
		
	}

}